/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Tag;

/**
 * An instance of TagDAO is a data access object used to access Tag, which
 * contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class TagDAO extends BaseDAO {

	@Override
	public void deleteSingle(Object persistentInstance) {

		if (persistentInstance instanceof Tag) {
			Integer tagId = ((Tag) persistentInstance).getId();
			MessageTagsDAO messageTagsdao = new MessageTagsDAO();
			messageTagsdao.deleteByProperty("tagid", tagId);

		}
		super.deleteSingle(persistentInstance);
	}

	public List<Tag> findAll(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Tag";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Tag> results = queryObject.list();

		return results;

	}

	public List<Tag> findBy2Properties(String propertyName1, Object value1,
			String propertyName2, Object value2) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Tag as model where model." + propertyName1
				+ "= ? and model." + propertyName2 + "= ? order by model.rankv";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value1);
		queryObject.setParameter(1, value2);
		@SuppressWarnings("unchecked")
		List<Tag> results = queryObject.list();

		return results;

	}

	public Tag findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Tag instance = (Tag) session.get(Tag.class.getName(), id);

		return instance;

	}

	public List<Tag> findByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Tag as model where model." + propertyName
				+ "= ? order by model.rankv";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<Tag> results = queryObject.list();

		return results;

	}

	public List<Tag> findByProperty(String propertyName, Object value,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Tag as model where model." + propertyName
				+ "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Tag> results = queryObject.list();

		return results;

	}

	public Integer findContainerId(java.lang.Integer tagid) {

		Session session = HibernateSessionFactory.getSession();
		Tag instance = (Tag) session.get(Tag.class.getName(), tagid);

		return instance != null ? instance.getTcid() : null;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Tag as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

}